# @Time : 2021/8/7 16:48
# @Author : Li Kunlun
# @Description : Adam算法

import utils as d2l
from mxnet import nd

# 1、从零开始实现
features, labels = d2l.get_data_ch7()


# 按照Adam算法中的公式实现该算法。其中时间步 𝑡 通过hyperparams参数传入adam函数
def init_adam_states():
    v_w, v_b = nd.zeros((features.shape[1], 1)), nd.zeros(1)
    s_w, s_b = nd.zeros((features.shape[1], 1)), nd.zeros(1)
    return ((v_w, s_w), (v_b, s_b))


def adam(params, states, hyperparams):
    beta1, beta2, eps = 0.9, 0.999, 1e-6
    for p, (v, s) in zip(params, states):
        v[:] = beta1 * v + (1 - beta1) * p.grad
        s[:] = beta2 * s + (1 - beta2) * p.grad.square()
        v_bias_corr = v / (1 - beta1 ** hyperparams['t'])
        s_bias_corr = s / (1 - beta2 ** hyperparams['t'])
        p[:] -= hyperparams['lr'] * v_bias_corr / (s_bias_corr.sqrt() + eps)
    hyperparams['t'] += 1


# 使用学习率为0.01的Adam算法来训练模型
d2l.train_ch7(adam, init_adam_states(), {'lr': 0.01, 't': 1}, features, labels)

print("--------------------简洁实现-----------------------------------")
d2l.train_gluon_ch7('adam', {'learning_rate': 0.01}, features, labels)
